import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import babel from 'rollup-plugin-babel';
import { terser } from 'rollup-plugin-terser';
import serve from 'rollup-plugin-serve';
import pkg from './package.json';
// 新增 rollup-plugin-postcss 插件
import postcss from 'rollup-plugin-postcss';
import nested from 'postcss-nested';
import cssnano from 'cssnano';

const isProduction = process.env.NODE_ENV === 'production';

export default [
    {
        input: 'src/js/index.js',
        output: [
            { file: pkg.main, format: 'cjs' },
            { file: pkg.module, format: 'es' },
            { file: pkg.browser, format: 'umd', name: 'Table', sourcemap: true },
        ],
        plugins: [
            resolve(),  // 这样 Rollup 能找到 `ms`
            commonjs(), // 这样 Rollup 能转换 `ms` 为一个ES模块
            babel({
                exclude: 'node_modules/**',
                // 使plugin-transform-runtime生效
                runtimeHelpers: true,
            }),
            postcss({
                extensions: ['.css'],
                plugins: [
                    nested(),
                    cssnano()
                ]
            }),
            // 生产环境才进行代码压缩
            isProduction && terser(),
            // 开启预览服务
            serve({
                open: true, // 是否打开浏览器
                contentBase: './public', // 入口html的文件位置
                historyApiFallback: true, // Set to true to return index.html instead of 404
                host: 'localhost',
                port: 1111
            })
        ]
    },
];
